home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / DEV / C-H / CMacPrimer.cpt / Flying Line ƒ / flying line.c < prev   
C/C++ Source or Header  |  1989-12-16  |  3KB  |  161 lines

  1. #define NUM_LINES            50
  2. #define NIL_POINTER            0L
  3. #define MOVE_TO_FRONT        -1L
  4. #define REMOVE_ALL_EVENTS    0
  5. #define NIL_STRING            "\P"
  6. #define NIL_TITLE            NIL_STRING
  7. #define VISIBLE                TRUE
  8. #define NO_GO_AWAY            FALSE
  9. #define NIL_REF_CON            NIL_POINTER
  10.  
  11. WindowPtr    gLineWindow;
  12. Rect        gLines[NUM_LINES];
  13. int            gDeltaTop=3, gDeltaBottom=4;
  14. int            gDeltaLeft=2, gDeltaRight=6;
  15. int            gOldMBarHeight;
  16.  
  17. main()
  18. {
  19.     ToolBoxInit();
  20.     WindowInit();
  21.     LinesInit();
  22.     MainLoop();
  23. }
  24.  
  25. ToolBoxInit()
  26. {
  27.     InitGraf(&thePort);
  28.     InitFonts();
  29.     FlushEvents(everyEvent,REMOVE_ALL_EVENTS);
  30.     InitWindows();
  31.     InitMenus();
  32.     TEInit();
  33.     InitDialogs(NIL_POINTER);
  34.     InitCursor();
  35. }
  36.  
  37. WindowInit()
  38. {
  39.     Rect        totalRect, mBarRect;
  40.     RgnHandle    mBarRgn;
  41.     
  42.     gOldMBarHeight = MBarHeight;
  43.     MBarHeight=0;
  44.     gLineWindow = NewWindow(NIL_POINTER, &(screenBits.bounds), 
  45.         NIL_TITLE, VISIBLE, plainDBox, MOVE_TO_FRONT, NO_GO_AWAY,
  46.         NIL_REF_CON);
  47.         
  48.     SetRect( &mBarRect, screenBits.bounds.left, screenBits.bounds.top,
  49.         screenBits.bounds.right, screenBits.bounds.top+gOldMBarHeight);
  50.     mBarRgn = NewRgn();
  51.     RectRgn(mBarRgn, &mBarRect);
  52.     UnionRgn(gLineWindow->visRgn, mBarRgn, gLineWindow->visRgn);
  53.     DisposeRgn(mBarRgn);
  54.  
  55.     SetPort(gLineWindow);
  56.     FillRect(&(gLineWindow->portRect), black);
  57.     PenMode(patXor);
  58. }
  59.  
  60. LinesInit()
  61. {
  62.     int i;
  63.     
  64.     HideCursor();
  65.     GetDateTime(&randSeed);
  66.     RandomRect(&(gLines[0]), gLineWindow);
  67.     DrawLine(0);
  68.     for(i=1; i<NUM_LINES; i++)
  69.     {
  70.         gLines[i] =gLines[i-1];
  71.         RecalcLine(i);
  72.         DrawLine(i);
  73.     }
  74. }
  75.  
  76. MainLoop()
  77. {
  78.     int i;
  79.     
  80.     while(!Button())
  81.     {
  82.         DrawLine(NUM_LINES-1);
  83.         for(i=NUM_LINES-1; i>0;i--)
  84.             gLines[i] = gLines[i-1];
  85.         RecalcLine(0);
  86.         DrawLine(0);
  87.     }
  88.     MBarHeight = gOldMBarHeight;
  89. }
  90.  
  91. RandomRect (myRectPtr, boundingWindow)
  92. Rect        *myRectPtr;
  93. WindowPtr    boundingWindow;
  94. {
  95.     myRectPtr->left = Randomize(boundingWindow->portRect.right
  96.         - boundingWindow->portRect.left); 
  97.     myRectPtr->right = Randomize(boundingWindow->portRect.right
  98.         - boundingWindow->portRect.left); 
  99.     myRectPtr->top = Randomize(boundingWindow->portRect.bottom
  100.         - boundingWindow->portRect.top); 
  101.     myRectPtr->bottom = Randomize(boundingWindow->portRect.bottom
  102.         - boundingWindow->portRect.top);
  103. }
  104.  
  105. Randomize(range)
  106. int range;
  107. {
  108.     long    rawResult;
  109.     
  110.     rawResult= Random();
  111.     if(rawResult<0)rawResult*= -1;
  112.     return((rawResult*range)/32768);
  113. }
  114.  
  115. RecalcLine(i)
  116. int i;
  117. {
  118.     gLines[i].top += gDeltaTop;
  119.     if(( gLines[i].top< gLineWindow->portRect.top) ||
  120.         (gLines[i].top> gLineWindow->portRect.bottom))
  121.     {
  122.         gDeltaTop *= -1;
  123.         gLines[i].top += 2*gDeltaTop;
  124.     }
  125.     
  126.     gLines[i].bottom += gDeltaBottom;
  127.     if(( gLines[i].bottom< gLineWindow->portRect.top) ||
  128.         (gLines[i].bottom> gLineWindow->portRect.bottom))
  129.     {
  130.         gDeltaBottom *= -1;
  131.         gLines[i].bottom += 2*gDeltaBottom;
  132.     }
  133.     
  134.     gLines[i].left += gDeltaLeft;
  135.     if(( gLines[i].left< gLineWindow->portRect.left) ||
  136.         (gLines[i].left> gLineWindow->portRect.right))
  137.     {
  138.         gDeltaLeft *= -1;
  139.         gLines[i].left += 2*gDeltaLeft;
  140.     }
  141.     
  142.     gLines[i].right += gDeltaRight;
  143.     if(( gLines[i].right< gLineWindow->portRect.left) ||
  144.         (gLines[i].right> gLineWindow->portRect.right))
  145.     {
  146.         gDeltaRight *= -1;
  147.         gLines[i].right += 2*gDeltaRight;
  148.     }
  149. }
  150.  
  151. DrawLine(i)
  152. int    i;
  153. {
  154.     MoveTo(gLines[i].left, gLines[i].top);
  155.     LineTo(gLines[i].right, gLines[i].bottom);
  156. }
  157.  
  158.     
  159.     
  160.      
  161.